import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;

import twitter4j.Status;

public class Node implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6346446236446971815L;
	private Status val;
	private Node pere;
	private ArrayList<Node> fils;

	public Node(Status v) {
		val = v;
		pere = null;
		fils = new ArrayList<Node>();
	}

	public Node(Status v, Node p) {
		val = v;
		pere = p;
		fils = new ArrayList<Node>();
	}

	public void ajouterFils(Status s) {
		Node n = new Node(s, this);
		this.fils.add(n);
	}

	public Status getVal() {
		return val;
	}

	public void setVal(Status val) {
		this.val = val;
	}

	public Node getPere() {
		return pere;
	}

	public void setPere(Node pere) {
		this.pere = pere;
	}

	public ArrayList<Node> getFils() {
		return fils;
	}

	public void setFils(ArrayList<Node> fils) {
		this.fils = fils;
	}

	/*
	 * Put in a string all the tweets from a given node
	 */

	public String getText() {
		String s = "Arbre de tweets :\n" + getVal().getUser().getName() + ":"
				+ getVal().getText();
		int i = 0;
		int j = 0;
		int k = 0;
		ArrayList<Node> suivants = new ArrayList<Node>();
		System.out.println(fils.size());
		while (i < fils.size()) {
			j = 0;
			while (j < fils.get(i).getFils().size()) {
				suivants.add(fils.get(i).getFils().get(j));
				j++;
			}
			s += "\n" + fils.get(i).getVal().getUser().getName() + ":"
					+ fils.get(i).getVal().getText();
			i++;

		}
		System.out.println(suivants.size());
		while (k < suivants.size()) {

			s += "\n" + suivants.get(k).getVal().getUser().getName() + ":"
					+ suivants.get(k).getVal().getText();
			k++;
		}
		return s;

	}

	/*
	 * Breadth-first search
	 */

	public void saveTree() {
		int i;
		LinkedList<Node> fifo = new LinkedList<Node>();
		fifo.add(this);
		while (!fifo.isEmpty()) {
			i = 0;
			Node x = fifo.remove();
			x.save();
			while (i < x.getFils().size()) {
				Node z = x.getFils().get(i);
				fifo.add(z);
				i++;
			}
		}
	}

	/*
	 * Save & Load Tree Call this methods on root only.
	 */

	public void save() {
		try {
			FileOutputStream fos = new FileOutputStream("cache/trees/"+val.getId()+"tree.tw");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(val);
			oos.writeObject(pere);
			oos.writeObject(fils);
			oos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void load() {
		try {
			FileInputStream fis = new FileInputStream("cache/trees/"+val.getId()+"tree.tw");
			ObjectInputStream ois = new ObjectInputStream(fis);
			val = (Status) ois.readObject();
			pere = (Node) ois.readObject();
			fils = (ArrayList<Node>) ois.readObject();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
